package com.kuaiyin.controller.request;


import com.kuaiyin.dto.RequestDTO;
import com.kuaiyin.entity.Request;
import com.kuaiyin.result.Result;
import com.kuaiyin.service.IRequestService;
import com.kuaiyin.utils.CurlParser;
import com.kuaiyin.utils.PageResult;
import com.kuaiyin.vo.RequestVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 接口请求定义表（支持动态模板） 前端控制器
 * </p>
 *
 * @author ng
 * @since 2025-04-25
 */
@Slf4j
@Tag(name = "请求管理")
@RestController
@RequestMapping("/request")
public class RequestController {
    @Autowired
    private IRequestService requestService;

    /**
     * 新增请求
     *
     * @param requestDTO 请求参数
     * @return  新增成功
     */
    @Operation(summary ="新增请求")
    @PostMapping()
    public Result<String> saveRequest(@Valid  @RequestBody  RequestDTO requestDTO) {
        requestService.savaRequest(requestDTO);
        return Result.success("新增成功");
    }

    @Operation(summary ="查询请求详情")
    @GetMapping("/{id}")
    public Result<RequestVO> getRequest(@PathVariable Long id) {
        RequestVO requestVO = requestService.getById(id);
        return Result.success(requestVO);
    }

    @Operation(summary ="更新请求")
    @PutMapping("/{id}")
    public Result<String> updateRequest(@PathVariable Long id,@Valid @RequestBody RequestDTO requestDTO) {
        requestService.updateRequest(id, requestDTO);
        return Result.success("更新成功");
    }

    @Operation(summary ="删除请求")
    @DeleteMapping("/{id}")
    public Result<String> deleteRequest(@PathVariable Long id) {
        requestService.removeById(id);
        return Result.success("删除成功");
    }

    @Operation(summary ="查询请求列表")
    @GetMapping("/all")
    public Result<List<RequestVO>> getRequestList() {
        List<RequestVO> requestList =requestService.getRequestList();
        return Result.success(requestList);
    }

    @Operation(summary ="分页查询请求列表")
    @GetMapping("/search")
    public Result<PageResult<RequestVO>> getRequest(
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        PageResult<RequestVO> userPage =requestService.getRequestPage( page, size,
                wrapper -> {
                    if (StringUtils.hasText(name)) {
                        wrapper.like(Request::getName, name);
                    }
                    wrapper.orderByDesc(Request::getId);
                });
        return Result.success(userPage);
    }

    @Operation(summary = "通过 curl 导入接口定义")
    @PostMapping("/import-curl")
    public Result<RequestDTO> importCurl(@RequestParam String name, @RequestParam String curlCommand) {
        RequestDTO dto = CurlParser.parse(curlCommand);
        dto.setName(name);
        requestService.savaRequest(dto);
        return Result.success("新增成功",dto);
    }
}
